﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

import asgard.config.ConfigCollector;

import ewhiteboard.events.EventList;

import mars.process.application.RunConfig;
import mars.process.application.RunDebug;

import pegas.events.ActionEvent;
import pegas.process.ActionProxy;
import pegas.process.Sequencer;
import pegas.process.SimpleAction;

import vegas.events.Delegate;
import vegas.events.Event;
import vegas.events.EventDispatcher;

/**
 * This controller run the application in the Application class.
 * @author eKameleon
 */
class ewhiteboard.process.application.RunApplication extends SimpleAction
{
	
	/**
	 * Creates a new RunApplication instance.
	 */
	function RunApplication() 
	{

	}

	/**
	 * Returns the singleton reference of this class.
	 * @return the singleton reference of this class.
	 */
	public static function getInstance():RunApplication
	{
		if ( _instance == null )
		{
			_instance = new RunApplication() ; 
		}	
		return _instance ;
	}
	
	/**
	 * Handles the event.
	 */
	public function run(e:Event):Void 
	{
		
		getLogger().debug( this + " handleEvent.") ;
		
		var seq:Sequencer = new Sequencer() ;
		
		seq.addEventListener( ActionEvent.START  , new Delegate(this, notifyStarted ) , null, null, true) ;
		seq.addEventListener( ActionEvent.FINISH , new Delegate(this, notifyFinished ) , null, null, true) ;

		seq.addAction( new RunConfig() ) ;
		seq.addAction( new ActionProxy( ConfigCollector, ConfigCollector.run ) ) ;
		seq.addAction( new RunDebug() ) ;
				
		seq.run() ;
		
	}

	/**
	 * Invoqued when the internal sequencer of this controller finished.
	 */
	public function notifyFinished( e:ActionEvent ):Void
	{
		getLogger().debug( this + " is finished." ) ;
		EventDispatcher.getInstance().dispatchEvent( EventList.INIT_APPLICATION ) ;
	}

	/**
	 * Invoqued when the internal sequencer of this controller started.
	 */
	public function notifyStarted( e:ActionEvent ):Void
	{
		getLogger().debug( this + " is started." ) ;
	}
	
	/**
	 * The internal singleton reference of this class.
	 */
	private static var _instance:RunApplication ;
	
}